// String.trim
String.prototype.trim = function() {
	return this.replace(/^[\s\r\n\t]+|[\s\r\n\t]+$/g, '');
}

// onload
$(function(){
	
	// datepicker
	$.datepicker.setDefaults($.extend($.datepicker.regional['sr-SR'], {
		dateFormat: "yy-mm-dd"
	}));
	$('.datepicker').datepicker({
		changeMonth: true,
		changeYear:  true
	});
	
	// multi inputs
	$('.multi-inputs').each(function(){
		var container_id = this.id;
		$(this).children(".item").each(function(){
			// add
			var add = $('<a class="btn add">+</a>');
			add.data("container_id", container_id);
			add.click(function(){
				var item = $(this).parent();
				var nItem = item.clone(true);
				item.after(nItem);
				RebuildIndexes(item.parent());
				
				var container_func_name = $(this).data("container_id") + "_onAdd";
				if(eval('typeof('+container_func_name+')') == "function") {
					eval(container_func_name+'(nItem)');
				}
			});
			$(this).append(add);
			
			// remove
			var remove = $('<a class="btn delete">-</a>');
			remove.data("container_id", container_id);
			remove.click(function(){
				var item = $(this).parent();
				var container = $(this).parent().parent();
				var container_func_name = $(this).data("container_id") + "_onDelete";
				$(this).parent().remove();
				RebuildIndexes(container);
				
				if(eval('typeof('+container_func_name+')') == "function") {
					eval(container_func_name+'(item)');
				}
			});
			$(this).append(remove);
		});
	});
});

function RebuildIndexes(container) {
	container.find(".item").each(function(index){
		$(this).find("input[name],select,textarea").each(function(){
			$(this).attr("name", $(this).attr("name").replace(/\[\d+\]/, '[' + index + ']'));
		});
	})
}

function number_format (number, decimals, dec_point, thousands_sep) {
	number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
	var n = !isFinite(+number) ? 0 : +number;
	var prec = !isFinite(+decimals) ? 2 : Math.abs(decimals);
	var sep = (typeof thousands_sep === 'undefined') ? '.' : thousands_sep;
	var dec = (typeof dec_point === 'undefined') ? ',' : dec_point;
	var s = '';
	
	var toFixedFix = function (n, prec) {
		var k = Math.pow(10, prec);
		return '' + Math.round(n * k) / k;
	};
	
	// Fix for IE parseFloat(0.55).toFixed(0) = 0;
	s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
	if (s[0].length > 3) {
		s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
	}
	if ((s[1] || '').length < prec) {
		s[1] = s[1] || '';
		s[1] += new Array(prec - s[1].length + 1).join('0');
	}
	return s.join(dec);
}

function popUp(url, w, h) {
	var options = {
		width:    w,
		height:   h,
		scrollbars: 1
	}
	var opts = '';
	for(x in options) {
		opts += x+'='+options[x]+','
	}
	var w = window.open(url, 'popup', opts);
	w.focus();
}

function lightBox(url, width, height, title) {
	var div = $('#lightbox');
	if(!div.length) {
		div = $('<div id="lightbox">').appendTo(document.body);
	}
	
	width  = parseInt(width)>0  ? width  : 600;
	height = parseInt(height)>0 ? height : 400;
	
	div.dialog({
		width:  width,
		height: height,
		modal:  true,
		title:  title,
		resizable: false
	});
	
	$.ajax({
		url: url,
		beforeSend: function() {
			div.addClass('loading');
		},
		complete: function(req) {
			div.removeClass('loading');
			if (req.status==200) {
				div.html(req.responseText)
			}
		}
	})
	
}